/**
 * Created by wange21 on 2017/7/3.
 */

(function (window,$) {
        "use strict";

        var dom;
        var total;
        var now;
        var next;

        var page = {
            pre: '<li><a>首页</a></li><li><a>上一页</a></li>',
            com: '<li><a>1</a></li>',
            next: '<li><a>下一页</a></li><li><a>末页</a></li>'
        };

        var version = "v0.2.0",
            sPage = function (d, u) {
                return new sPage.fn.init(d, u);
            };

        sPage.fn = sPage.prototype = {
            spage: version,
            constructor: sPage
        };

        var init = sPage.fn.init = function (obj) {
            dom = obj.dom;
            now = obj.now;
            total = obj.total;
            // $.get(url, function (json) {
            //     var data = $.parseJSON(json);
            //     total = data.last_page;
            //     sPage.gen();
            //     return "OK";
            // });
        };

        sPage.prototype.init.prototype = sPage.prototype;

        sPage.gen = function () {
            dom.html(page.pre);
            if (total < 6) {
                for (var i = 1; i <= total; i++) {
                    dom.append(page.com);
                    dom.children().last().children().text(i);
                }
                dom.children().eq(now + 1).addClass("disabled").addClass(
                    'active');
            } else if (total - now < 6) {
                for (i = total - 5; i <= total; i++) {
                    dom.append(page.com);
                    dom.children().last().children().text(i);
                }
                dom.children().eq(now - total + 7).addClass("disabled")
                    .addClass('active');
            } else {
                for (i = now; i < now + 6; i++) {
                    dom.append(page.com);
                    dom.children().last().children().text(i);
                }
                dom.children().eq(2).addClass("disabled").addClass('active');
            }
            dom.append(page.next);
            if (now === 1) {
                dom.children().eq(0).addClass("disabled");
                dom.children().eq(1).addClass("disabled");
            }
            if (now === total|| total === 0) {
                dom.children().last().addClass("disabled");
                dom.children().last().prev().addClass("disabled");
            }
        };

        sPage.bind = function (fun) {
            dom.delegate("li", "click", function () {
                if (!$(this).hasClass("disabled")) {
                    switch ($(this).text()) {
                        case "首页": {
                            now = 1;
                            break;
                        }
                        case "上一页": {
                            now--;
                            break;
                        }
                        case "下一页": {
                            now++;
                            break;
                        }
                        case "末页": {
                            now = total;
                            break;
                        }
                        default: {
                            now = parseInt($(this).text());
                            break;
                        }
                    }
                    sPage.gen();
                    fun();
                }
            });
        };

        sPage.getnow = function () {
            return now;
        };

        window.sPage = sPage;
    })(window,jQuery);
